﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;

namespace Photo.Web.Core.Setup
{
    /// <summary>
    /// 日志扩展集成
    /// </summary>
    public static class LogSetup
    {
        /// <summary>
        /// 添加日志组件扩展集成
        /// </summary>
        /// <param name="services"></param>
        public static void AddLogSetup(this IServiceCollection services)
        {
            //全局webapi监听日志
            services.AddMvcFilter<LoggingMonitorAttribute>();

            //添加日志输入到文件配置,每天创建一个日志文件
            services.AddFileLogging("logs/{0:yyyy}-{0:MM}-{0:dd}/info.log", options =>
            {
                options.WriteFilter = (logMsg) =>
                {
                    return logMsg.LogLevel == LogLevel.Information;
                };
                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.UtcNow);
                };
                options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
            });
            services.AddFileLogging("logs/{0:yyyy}-{0:MM}-{0:dd}/error.log", options =>
            {
                options.WriteFilter = (logMsg) =>
                {
                    return logMsg.LogLevel == LogLevel.Error;
                };
                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.UtcNow);
                };
                options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
            });
            services.AddFileLogging("logs/{0:yyyy}-{0:MM}-{0:dd}/warning.log", options =>
            {
                options.WriteFilter = (logMsg) =>
                {
                    return logMsg.LogLevel == LogLevel.Warning;
                };
                options.FileNameRule = fileName =>
                {
                    return string.Format(fileName, DateTime.UtcNow);
                };
                options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
            });

        }
    }
}
